package com.jinke.uang.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jinke.uang.R;
import com.jinke.uang.adapter.OrderAdapter;
import com.jinke.uang.base.BaseResponse;
import com.jinke.uang.bean.BorrowingData;
import com.jinke.uang.constant.HttpConstant;
import com.jinke.uang.model.MineModule;
import com.jinke.uang.presenter.CommonPresenter;
import com.jinke.uang.bean.OrderItemData;
import com.jinke.uang.utils.PXUtils;
import com.jinke.uang.view.MyItemDecoration;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 个人中心的fragment 借款已退出的订单
 */
public class BorrowFinishFragment extends BaseNewFragment<CommonPresenter,MineModule> {


    Unbinder unbinder;

    private List<OrderItemData> datas = new ArrayList<>();
    @BindView(R.id.rv)
    RecyclerView rv;

    @BindView(R.id.linNoData)
    LinearLayout linNoData;
    @BindView(R.id.tvNoDataText)
    TextView tvNoDataText;

    private OrderAdapter adapter;
    private final String PAGE ="1";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_borrow_finish, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        initData();
        return rootView;
    }

    private void initData() {
        mPresenter.getData(HttpConstant.ORDER_DATA2,"33","3",PAGE);
        mPresenter.getData(HttpConstant.ORDER_DATA2,"22","2",PAGE);
        showLoadingView();
    }

    @Override
    protected CommonPresenter getPresenter() {
        return new CommonPresenter();
    }

    @Override
    protected MineModule getModule() {
        return new MineModule();
    }

    @Override
    public void onFirstVisible(Bundle savedInstanceState) {
        super.onFirstVisible(savedInstanceState);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        MyItemDecoration dividerItemDecoration = new MyItemDecoration(PXUtils.dp2px(getActivity()
                , 10));
        rv.addItemDecoration(dividerItemDecoration);
        rv.setLayoutManager(layoutManager);
        adapter = new OrderAdapter(getActivity(), datas);
        rv.setAdapter(adapter);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    //返回值
    @Override
    public void showResponse(int apiType, String listType, Object o) {
        if(apiType== HttpConstant.CALCULATED_ROLL_OVER_DATA){ //展期数据计算接口
        }

        else if(apiType==HttpConstant.ORDER_DATA2){ //获取已结清订单
            BaseResponse<BorrowingData> response = (BaseResponse<BorrowingData>) o;
            if(listType.equals("33")){
                dismissLoadingView();
                 datas.addAll(response.getData().getRecords());
            }

            if(listType.equals("22")){
                dismissLoadingView();
                datas.addAll(response.getData().getRecords());
            }

            if(null!=datas&&datas.size()>0){
                adapter.notifyDataSetChanged();
                rv.setVisibility(View.VISIBLE);
                linNoData.setVisibility(View.GONE);
            }else{
                rv.setVisibility(View.GONE);
                linNoData.setVisibility(View.VISIBLE);
                tvNoDataText.setText(R.string.nodata_borrowfinish);
            }
        }

        else if(apiType==HttpConstant.USER_INFO_BACK){  //用户资料回退

        }

        else if(apiType==HttpConstant.ORDER_DATA){ //根据订单类型获取数据

        }
    }

    @Override
    public void showError(Throwable e) {
        rv.setVisibility(View.GONE);
        linNoData.setVisibility(View.VISIBLE);
        tvNoDataText.setText(R.string.nodata_borrowfinish);
    }
}
